/**
 * 
 */
package com.java.test.datacenter.mapper;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.java.test.audit.dto.RemitPaymentInfo;
import com.java.test.datacenter.dto.PaymentInfo;
import com.java.test.user.dto.ReflectInfo;
import com.java.test.util.PageInfo;

/**
 * @author Administrator
 *
 */
@Component
public class PaymentInfoMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int insertPaymentInfo(PaymentInfo info) {

		String sql = "INSERT INTO datamanage_payment "
				+ "(`remit_id`, `user_id`, `user_name`, `amount`, `new_outTime`) " + "VALUES (?,?,?,?,?)";

		int i = jdbcTemplate.update(sql, info.getRemitId(), info.getUserId(), info.getUserName(), info.getAmount(),
				info.getNewOutTime());

		return i;
	}

	public Double curToDaySumAmount(ReflectInfo info) {
		String sql = "select sum(reflect_amount) from member_reflect where 1=1 ";

//		if ((info.getStartTime() == null || "".equals(info.getStartTime()))
//				&& (info.getEndTime() == null || "".equals(info.getEndTime()))) {
//			sql += "and  TO_DAYS(reflect_time) = TO_DAYS(NOW())";
//		}

		if ((info.getStartTime() == null || "".equals(info.getStartTime()))) {
			sql += " and  TO_DAYS(reflect_time) = TO_DAYS(NOW())";
		}

		if ((info.getStartTime() != null || !"".equals(info.getStartTime()))
				&& (info.getEndTime() == null || "".equals(info.getEndTime()))) {
			sql += " and  TO_DAYS(reflect_time) = TO_DAYS(NOW())";
		}

		if (info.getStartTime() != null && !"".equals(info.getStartTime())
				&& (info.getEndTime() != null && !"".equals(info.getEndTime()))) {
			sql += " and reflect_time>='" + info.getStartTime() + "' and reflect_time<='" + info.getEndTime() + "'";

		}

		Double i = jdbcTemplate.queryForObject(sql, Double.class);

		if (i == null) {
			return Double.valueOf(0.00d);
		}

		return i;
	}

	public List<RemitPaymentInfo> selectremitInfoById(PaymentInfo info) {
		String sql = "select a.user_name,a.new_outTime," + "b.* from datamanage_payment a left join "
				+ "examine_remit b on a.remit_id=b.id where a.user_id=?";
		LocalDateTime startTime = info.getStartTime();
		LocalDateTime endTime = info.getEndTime();
		
		if (startTime == null && endTime == null) {
			sql += " and TO_DAYS(a.new_outTime)=TO_DAYS('" + LocalDateTime.now() + "')";
		} else {
			if (startTime != null && endTime == null) {
				sql += " and TO_DAYS(a.new_outTime)=TO_DAYS('" + startTime + "')";
			}
			if (startTime != null && endTime != null) {
				sql += " and a.new_outTime>='" + startTime + "' and a.new_outTime<='" + endTime + "'";
			}
		}
		sql+=" order by a.new_outTime desc";
		
		return jdbcTemplate.query(sql, new RemitPaymentInfo(), info.getUserId());
	}

	// 入款统计
	public int selectpaymentCount(PaymentInfo info) {
		String sql = "select count(*) from (select user_name,count(*),sum(amount),max(new_outTime) from datamanage_payment where 1=1  ";

		if (info.getStartTime() != null) {
			sql += " and new_outTime>='" + info.getStartTime() + "'";
		}

		if (info.getEndTime() != null) {
			sql += " and new_outTime<='" + info.getEndTime() + "'";
		}

		sql += " group by user_id) a ";
		int count = jdbcTemplate.queryForObject(sql, Integer.class);
		return count;
	}

	public int selectCount(PaymentInfo info) {
		String sql = "select count(*) from (select max(user_name) as user_name,count(*) as count,sum(amount) as amount,max(new_outTime) as new_out_time from datamanage_payment  where 1=1 ";

		if (info.getStartTime() == null && info.getEndTime() == null) {
			sql += " and  TO_DAYS(new_outTime) = TO_DAYS(NOW())";
		} else {

			if (info.getStartTime() != null && info.getEndTime() != null) {
				sql += " and new_outTime>='" + info.getStartTime() + "' and new_outTime<='" + info.getEndTime() + "'";
			} else {
				sql += " and  TO_DAYS(new_outTime) = TO_DAYS('" + info.getStartTime() + "')";
			}

		}

		String sql2 = " group by user_id) a";
		Integer i = jdbcTemplate.queryForObject(sql + sql2, Integer.class);

		return i == null ? null : i;

	}

	public List<PaymentInfo> selectpaymentList(PageInfo<PaymentInfo> page, PaymentInfo info) {
		String sql = "select null as remit_id,user_id,user_name,count(*)as count,sum(amount) as amount,max(new_outTime)  as new_out_time from datamanage_payment where 1=1 ";

		if (info.getStartTime() == null && info.getEndTime() == null) {
			sql += " and  TO_DAYS(new_outTime) = TO_DAYS(NOW())";
		} else {

			if (info.getStartTime() != null && info.getEndTime() != null) {
				sql += " and new_outTime>='" + info.getStartTime() + "' and new_outTime<='" + info.getEndTime() + "'";
			} else {
				sql += " and  TO_DAYS(new_outTime) = TO_DAYS('" + info.getStartTime() + "')";
			}

		}

		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();
		sql += " group by user_id,user_name limit ?,?";
		List<PaymentInfo> list = jdbcTemplate.query(sql, new PaymentInfo(), start, limit);
		return list;
	}

	public Double selectpaymentcount(PaymentInfo info) {
		String sql = "select sum(amount) from datamanage_payment where 1=1 ";

		if (info.getStartTime() == null && info.getEndTime() == null) {
			sql += " and  TO_DAYS(new_outTime) = TO_DAYS(NOW())";
		} else {

			if (info.getStartTime() != null && info.getEndTime() != null) {
				sql += " and new_outTime>='" + info.getStartTime() + "' and new_outTime<='" + info.getEndTime() + "'";
			} else {
				sql += " and  TO_DAYS(new_outTime) = TO_DAYS('" + info.getStartTime() + "')";
			}

		}

		Double d = jdbcTemplate.queryForObject(sql, Double.class);

		return d;

	}

}
